package com.pengtu.web.project;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import com.pengtu.Constants;
import com.pengtu.entity.project.DrawingsContractInfo;
import com.pengtu.entity.project.SubentryInfo;
import com.pengtu.entity.project.SubprojectInfo;
import com.pengtu.entity.project.SubunitInfo;
import com.pengtu.service.project.ProjectManager;
import com.pengtu.service.project.SubManager;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

@Results({ @Result(name = BaseActionSupport.RELOAD, location = "/project/drawcontract!input.action?projectId=${projectId}&id=${id}", type = "redirect") })
public class DrawcontractAction extends BaseActionSupport<DrawingsContractInfo> {

	private static final long serialVersionUID = 1L;
	private ProjectManager projManager;
	private String id;
	private DrawingsContractInfo drawcontract;
	private List<DrawingsContractInfo> drawContractList = new ArrayList<DrawingsContractInfo>();
	private String projectId;
	private SubManager submanager;

	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		return INPUT;
	}

	/* 图纸合同段操作 */
	/**
	 * 
	 * showContract: 适用:填充TreeGrid中的数据
	 * 
	 * @return
	 * @exception
	 * @since 1.0.0
	 */
	public String showContract() {
		drawContractList = projManager.findDrawContractList(projectId);
		List<Map<String, Object>> drawContractSet = new ArrayList<Map<String, Object>>();
		if (drawContractList.size() != 0) {
			drawContractSet = projManager.fillTreeGrid(drawContractList);
			// 转换数据为Jkson格式
			Struts2Utils.renderJson(drawContractSet);
		}
		return NONE;
	}

	/**
	 * 
	 * getDrawContruct: 适用:查找单条图纸合同段信息
	 * 
	 * @return
	 * @exception
	 * @since 1.0.0
	 */
	public String getDrawContruct() {
		String drawId = Struts2Utils.getParameter("drawId");
		DrawingsContractInfo drawInfo = projManager
				.getDrowConract("id", drawId);
		Struts2Utils.renderJsontoExtOneData(drawInfo);
		return NONE;
	}

	/**
	 * 保存图纸合同段
	 */
	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		projManager.saveOrUpdateDrawContract(drawcontract);
		String mes = "";
		if(StringUtils.isNotEmpty(id)){
			mes = "修改";
		}else{
			mes = "新增";
		}
		addActionMessage(mes + drawcontract.getId() + "图纸合同段！");
		Struts2Utils.renderJson(Constants.EXT_SUCCESS_JSON_STR);
		return NONE;
	}

	/**
	 * 删除合同段信息
	 */
	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		// 查找将要删除的合同段下的子单位工程信息
		List<SubunitInfo> subunitList = submanager.findBySubunit(
				"drawingsContractId", id);
		if (subunitList.size() > 0) {
			for (SubunitInfo subunitInfo : subunitList) {
				List<SubprojectInfo> subprojectList = submanager
						.findBySubproject("subunitId", subunitInfo.getId());
				if (subprojectList.size() > 0) {
					for (SubprojectInfo subprojectInfo : subprojectList) {
						List<SubentryInfo> subentryList = submanager
								.findBySubentryInfo("subprojectId",
										subprojectInfo.getId());
						if (subentryList.size() > 0) {
							for (SubentryInfo subentryInfo : subentryList) {
								submanager.deleteSubentry(subentryInfo.getId());
							}
						}
						submanager.deleteSubproject(subprojectInfo.getId());
					}
				}
				submanager.deteleSubunit(subunitInfo.getId());
			}
		}
		projManager.deleteDrawContract(id);
		Struts2Utils.renderJson(Constants.EXT_SUCCESS_JSON_STR);
		return NONE;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(id)) {
			drawcontract = projManager.getDrowConract("id", id);
		} else {
			drawcontract = new DrawingsContractInfo();
		}
	}

	public String getProjectId() {
		return projectId;
	}

	public void setProjectId(String projectId) {
		this.projectId = projectId;
	}

	public DrawingsContractInfo getDrawcontract() {
		return drawcontract;
	}

	public List<DrawingsContractInfo> getDrawContractList() {
		return drawContractList;
	}

	public void setDrawContractList(List<DrawingsContractInfo> drawContractList) {

		this.drawContractList = drawContractList;

	}

	public void setId(String id) {
		this.id = id;
	}

	@Override
	public DrawingsContractInfo getModel() {
		// TODO Auto-generated method stub
		return drawcontract;
	}

	@Autowired
	public void setSubmanager(SubManager submanager) {
		this.submanager = submanager;
	}

	@Autowired
	public void setProjManager(ProjectManager projManager) {
		this.projManager = projManager;
	}

}
